{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "774a4aa4",
   "metadata": {},
   "source": [
    "# 切片和索引\n",
    "\n",
    "ndarray对象的内容可以通过索引或切片来访问和修改，与 Python 中 list 的切片操作一样。\n",
    "\n",
    "ndarray 数组可以基于 0 - n 的下标进行索引\n",
    "\n",
    "\n",
    ">区别在于：<font color='red'>数组切片是原始数组视图（这就意味着，如果做任何修改，原始都会跟着更改）。\n",
    "这也意味着，如果不想更改原始数组，我们需要进行显式的复制，从而得到它的副本（.copy())。</font>\n",
    "\n",
    "`冒号分隔切片参数 [start:stop:step]`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "de0dba92",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b6f9b792",
   "metadata": {},
   "source": [
    "## 一维数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "41047dff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(10)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "924337b1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 4, 6])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = a[2:7:2] # 从索引2开始到7结束，间隔为2\n",
    "b"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ec9723b2",
   "metadata": {},
   "source": [
    "冒号 : 的解释：如果只放置一个参数，\n",
    "- 如 [2]，将返回与该索引相对应的单个元素。\n",
    "- 如果为 [2:]，表示从该索引开始以后的所有项都将被提取。\n",
    "- 如果使用了两个参数，如 [2:7]，那么则提取两个索引(不包括停止索引)之间的项。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "80be206f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 4, 6])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[2:8:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "4252c2f2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2, 3, 4, 5, 6, 7, 8, 9])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[2:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "42945b26",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 4, 5, 6])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[:7] # 不包含末尾元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "c4b001b6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  3,  5,  7,  9, 11, 13, 15, 17, 19])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(1,20,2)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "d9ff29a8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "eac97945",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([19, 17, 15, 13, 11,  9,  7,  5,  3,  1])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[::-1] # 反着取值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "718442e8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1,  3,  5,  7,  9, 11, 13, 15])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[:-2:1] # 第一个到倒数第二个 倒数第二个不取"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "964bd182",
   "metadata": {},
   "source": [
    "## 为什么切片和区间会忽略最后一个元素\n",
    "计算机科学家edsger w.dijkstra(艾兹格·W·迪科斯彻)，delattr这一风格的解释应该是比较好的：\n",
    "- 当只有最后一个位置信息时，我们可以快速看出切片和区间里有几个元素：range(3)和my_list[:3]\n",
    "- 当起始位置信息都可见时，我们可以快速计算出切片和区间的长度，用有一个数减去第一个下表(stop-start)即可\n",
    "- 这样做也让我们可以利用任意一个下标把序列分割成不重叠的两部分，只要写成my_list[:x]和my_list[x:]就可以了。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d3c2edc5",
   "metadata": {},
   "source": [
    "## 二维数组\n",
    "### 同样使用上述索引提取方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "32c50835",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  3,  4,  5],\n",
       "       [ 6,  7,  8,  9, 10],\n",
       "       [11, 12, 13, 14, 15],\n",
       "       [16, 17, 18, 19, 20]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = np.arange(1,20+1).reshape(4,5)\n",
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "26427676",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.ndim # 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "0527b9dd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4, 5)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.shape # (4,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "960c4eec",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 3, 4, 5])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "8c15e5dd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 6,  7,  8,  9, 10])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "cbd68d01",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([11, 12, 13, 14, 15])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "eb728f17",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "13"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[2][2] # 13"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "6eb81896",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[11, 12, 13, 14, 15],\n",
       "       [16, 17, 18, 19, 20]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[2:] # 从第三行开始以后全都要"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7b16e537",
   "metadata": {},
   "source": [
    ">注意：切片还可以使用省略号“…”，如果在行位置使用省略号，那么返回值将包含所有行元素，反之，则包含所有列元素。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "3e4c4dc9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 3,  8, 13, 18])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[...,2] # 全体第三列元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "b7178435",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 3,  4,  5],\n",
       "       [ 8,  9, 10],\n",
       "       [13, 14, 15],\n",
       "       [18, 19, 20]])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[...,2:] # 从第三列开始截取 后面的都要"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "acd0942c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[2][2] == a[2,2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "ab95a6ed",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([11, 12, 13, 14, 15])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[...][2] # 取所有的行 然后取第三项"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "76912ddb",
   "metadata": {},
   "source": [
    "第一个`:,`代表第一个维度，`,`后面的表示取第二维的所有第几位数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "e41ec8ae",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  3,  4,  5],\n",
       "       [ 6,  7,  8,  9, 10],\n",
       "       [11, 12, 13, 14, 15],\n",
       "       [16, 17, 18, 19, 20]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "7e4a9f7a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 3,  8, 13, 18])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[:,2] # 取第三列数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "64c7d502",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2,  3,  4,  5],\n",
       "       [ 6,  7,  8,  9, 10],\n",
       "       [11, 12, 13, 14, 15],\n",
       "       [16, 17, 18, 19, 20]])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[...]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "2c2b96d9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 3,  8, 13, 18])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[...,2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "c3930841",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 3,  4,  5],\n",
       "       [ 8,  9, 10],\n",
       "       [13, 14, 15],\n",
       "       [18, 19, 20]])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[...,2:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "eb978416",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 3,  4],\n",
       "       [ 8,  9],\n",
       "       [13, 14],\n",
       "       [18, 19]])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[...,2:4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "a24b10d9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2],\n",
       "       [ 6,  7],\n",
       "       [11, 12],\n",
       "       [16, 17]])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[...,:2] # 第三列之前的全部数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "3053af24",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1,  2],\n",
       "       [ 6,  7],\n",
       "       [11, 12],\n",
       "       [16, 17]])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a[...,:2]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0c7e6676",
   "metadata": {},
   "source": [
    "# 总结\n",
    " - 即高维数组是以`，`来划分维度，逗号前是一个维度，逗号后是另外一个维度，互不干涉\n",
    " - `：`冒号是用来进行操作的，这里和一位数组类似。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5cc4b45e",
   "metadata": {},
   "source": [
    "# 索引的高级操作\n",
    "在 NumPy 中还可以使用高级索引方式，比如整数数组索引、布尔索引，以下将对两种种索引方式做详细介绍。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1a4d0a90",
   "metadata": {},
   "source": [
    "### 1. 整数数组索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "3d7f30f6",
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.array([\n",
    "    [1,2],\n",
    "    [3,4],\n",
    "    [5,6]\n",
    "])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "297d238d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python(numpy_learning)",
   "language": "python",
   "name": "numpy_learning"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.17"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
